#ifndef _SEND_MAIL_H_
#define _SEND_MAIL_H_

#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "change64.h"
#include "error.h"

#define SYS_ERROR -1
#define SMTP_ERROR -2
#define SMTP_SUCCESS 0

/*
 * 定义一个用于smtp发送邮件的类型
 */
typedef struct Smtp {
	char server_addr[105];
	char username[105];
	char passwd[105];
	char Recv[105];
	char buf[1024];
	struct timeval *connect_tv; //用于非阻塞连接超时的结构
	struct timeval *recv_tv; //用于接受超时的结构
	int sockfd;
}SMTP;

/*
 * 定义一个邮件类型，包括有标题，内容
 */
typedef struct mail {
	char title[105];
	char content[1024];
}Mail;

extern SMTP* smtp_init(void);
extern Mail* mail_init(void);
extern int set_server_addr(SMTP *smtpr, const char *server_addr);
extern int set_username(SMTP *smtpr, const char *username);
extern int set_passswd(SMTP *smtpr, const char *passwd);
extern int set_Recv(SMTP *smtpr, const char *Recv);
extern int set_connect_tv(SMTP *smtpr, int sec);
extern int set_recv_tv(SMTP *smtpr, int sec);
extern int set_title(Mail *smtpr, const char *title);
extern int set_content(Mail *smtpr, const char *content);
extern int read_msg(SMTP *smtpr);
extern int send_msg(SMTP *smtpr,char *buf);
extern int deal_return();
extern int connect_nonb(struct sockaddr* sp, socklen_t slen, 
 		int nsec, SMTP *smtpr);
extern int smtp_connect(struct sockaddr* sp, socklen_t slen, 
 		int nsec, SMTP *smtpr);
extern int hello(SMTP *smtpr, Mail *mpr);
extern int auth(SMTP *smtpr, Mail *mpr);
extern int login(SMTP *smtpr, Mail *mpr);
extern int from_to(SMTP *smtpr, Mail *mpr);
extern int data(SMTP *smtpr, Mail *mpr);
extern int mail(SMTP *smtpr, Mail *mpr);
extern int end(SMTP *smtpr, Mail *mpr);
extern int quit(SMTP *smtpr, Mail *mpr);
extern int send_mail(SMTP *smtpr, Mail *mpr);
extern void smtp_destory(SMTP *smtpr, Mail *mpr);

#endif
